package cn.com.ghy.android.bean;

import java.io.InputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import android.util.Xml;
import cn.com.ghy.android.util.CommonStatic;
import cn.com.ghy.android.util.XMLStatic;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:今天协访信息解析类
 * @author wuz  @date 2011-6-20 11:07:44
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class CoopVisitInfoBeanParser {
	
	public static String createXML(String vstype, String vsid, 
			String vsname, LoginInfoBean loginInfo, String vsdate,
			Writer writer) throws Exception {
        XmlSerializer xmlSerializer = Xml.newSerializer();
        
        xmlSerializer.setOutput(writer);
        xmlSerializer.startDocument("UTF-8", true);
        xmlSerializer.startTag("", XMLStatic.CONTENT);
        xmlSerializer.attribute("", XMLStatic.TOTAL_RECORD, "1");
        xmlSerializer.startTag("", XMLStatic.RECORDS);
        
        xmlSerializer.startTag("", XMLStatic.VSTYPE);
        xmlSerializer.text(vstype);
		xmlSerializer.endTag("", XMLStatic.VSTYPE);
		xmlSerializer.startTag("", XMLStatic.VSID);
        xmlSerializer.text(vsid);
		xmlSerializer.endTag("", XMLStatic.VSID);
		xmlSerializer.startTag("", XMLStatic.VSNAME);
        xmlSerializer.text(vsname);
		xmlSerializer.endTag("", XMLStatic.VSNAME);
		xmlSerializer.startTag("", XMLStatic.CUSER);
        xmlSerializer.text(loginInfo.getPernr());
		xmlSerializer.endTag("", XMLStatic.CUSER);
		xmlSerializer.startTag("", XMLStatic.VSDATE);
        xmlSerializer.text(vsdate);
		xmlSerializer.endTag("", XMLStatic.VSDATE);
		xmlSerializer.startTag("", XMLStatic.VSSAOF);
        xmlSerializer.text(loginInfo.getSaof());
		xmlSerializer.endTag("", XMLStatic.VSSAOF);
		
		xmlSerializer.endTag("", XMLStatic.RECORDS);
		xmlSerializer.endTag("", XMLStatic.CONTENT);
		xmlSerializer.endDocument();
		writer.flush();
		writer.close();
		return writer.toString().replace("<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>", "");
	}
	
	public static String createVisitTimeXML(String vstype, String vsid, 
			String vstime, LoginInfoBean loginInfo, String vsdate,
			Writer writer) throws Exception {
        XmlSerializer xmlSerializer = Xml.newSerializer();
        
        xmlSerializer.setOutput(writer);
        xmlSerializer.startDocument("UTF-8", true);
        xmlSerializer.startTag("", XMLStatic.CONTENT);
        xmlSerializer.attribute("", XMLStatic.TOTAL_RECORD, "1");
        xmlSerializer.startTag("", XMLStatic.RECORDS);
        
        xmlSerializer.startTag("", XMLStatic.VSTYPE);
        xmlSerializer.text(vstype);
		xmlSerializer.endTag("", XMLStatic.VSTYPE);
		xmlSerializer.startTag("", XMLStatic.VSID);
        xmlSerializer.text(vsid);
        xmlSerializer.endTag("", XMLStatic.VSID);
        if	(null != vstime) {
        	xmlSerializer.startTag("", XMLStatic.VSTIME);
            xmlSerializer.text(vstime);
    		xmlSerializer.endTag("", XMLStatic.VSTIME);
        }
		xmlSerializer.startTag("", XMLStatic.CUSER);
        xmlSerializer.text(loginInfo.getPernr());
		xmlSerializer.endTag("", XMLStatic.CUSER);
		xmlSerializer.startTag("", XMLStatic.VSDATE);
        xmlSerializer.text(vsdate);
		xmlSerializer.endTag("", XMLStatic.VSDATE);
		
		xmlSerializer.endTag("", XMLStatic.RECORDS);
		xmlSerializer.endTag("", XMLStatic.CONTENT);
		xmlSerializer.endDocument();
		writer.flush();
		writer.close();
		return writer.toString().replace("<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>", "");
	}
	
	public static List<CoopVisitInfoBean> parseXML(InputStream in, String vstype) throws Exception {
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(in, "UTF-8");
        int type = parser.getEventType(); 
        
        List<CoopVisitInfoBean> list = null;
        CoopVisitInfoBean bean = null;
        
        while (type != XmlPullParser.END_DOCUMENT) { 
            switch (type) {   
	            case XmlPullParser.START_DOCUMENT:   
	            	list = new ArrayList<CoopVisitInfoBean>();   
	                break;
	            case XmlPullParser.START_TAG:
	                if ("Table".equals(parser.getName())) {
	                	bean = new CoopVisitInfoBean();
	                } else if (null != bean) {
	                	if (vstype.equals(CommonStatic.VSTYPE_JXS)) {
		                    if ("KUNNR".equals(parser.getName())) {   
		                    	bean.setKunnr(parser.nextText());   
		                    } else if("KUNNRNAME".equals(parser.getName())) {
		                    	bean.setKunnrname(parser.nextText());
		                    } else if("ISVISIT".equals(parser.getName())) {
		                    	bean.setIsvisit(parser.nextText());
		                    } 
	                	} else if(vstype.equals(CommonStatic.VSTYPE_BIG) || vstype.equals(CommonStatic.VSTYPE_SMALL)) {
	                		if ("PARTNER".equals(parser.getName())) {   
		                    	bean.setKunnr(parser.nextText());   
		                    } else if("NAME_ORG1".equals(parser.getName())) {
		                    	bean.setKunnrname(parser.nextText());
		                    } else if("ISVISIT".equals(parser.getName())) {
		                    	bean.setIsvisit(parser.nextText());
		                    } else if("DENGJI".equals(parser.getName())) {
		                    	bean.setDengji(parser.nextText());
		                    }
	                	}
	                }   
	                break;
	            case XmlPullParser.END_TAG:   
	                if ("Table".equals(parser.getName())) {
	                    if (null != bean && null != list) {
	                    	list.add(bean);   
	                    	bean = null;   
	                    }   
	                }
	                break;
	        }
            type = parser.next();
        }   
        in.close();   
        return list;  
    }
}
